﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace prjCompras
{
    public partial class frmProcAprovarRequisicao : FormProcessoPadrao.FormProcessoPadrao
    {
        public frmProcAprovarRequisicao()
        {
            InitializeComponent();
        }

        string camposReq = @"r.Codigo, r.Data, r.Observacao, 
                             case when r.Urgente = 0 then 'Não' else 'Sim' end as Urgente";
        string camposMatReq = @"m.Codigo, m.Nome as Material, rm.Quantidade";

        private void frmProcAprovarRequisicao_Load(object sender, EventArgs e)
        {
            pnlFimProcesso.Visible = false;
            tbcntrlEtapas.Visible = false;
            pnlFimProcesso.Dock = DockStyle.Fill;
            pnlInicio.Dock = DockStyle.Fill;
            insereTextoProcesso(" Aprovar Requisição");

            string sql = "Select " + camposReq + " from " + ConstTabelas.REQUISIÇÃO + " as r where Tipo = 0 and Situacao = 0 order by Urgente Desc";
            dtgrdvwRequisicao.DataSource = Conexao.getInstance.executeQuery(sql);
            dtgrdvwRequisicao.Columns.Add("Acao", "Ação");
            dtgrdvwRequisicao.Columns[0].HeaderText = "Código";
            dtgrdvwRequisicao.Columns[2].HeaderText = "Observação";
        }

        private void dtgrdvwRequisicao_SelectionChanged(object sender, EventArgs e)
        {
            string codReq = "";
            if (dtgrdvwRequisicao.RowCount > 0)
            {
                codReq = dtgrdvwRequisicao.CurrentRow.Cells[0].Value.ToString();
            }
            string sql = @"Select " + camposMatReq + " from " + ConstTabelas.REQUISIÇÃO + " as r," + ConstTabelas.REQUISICAOMATERIAL +
                         @" as rm, " + ConstTabelas.MATERIAL + @" as m
                         where r.Codigo = rm.CodigoRequisicao 
                           and r.Codigo = '" + codReq + @"'
                           and m.Codigo = rm.CodigoMaterial";
            dtgrdvwMateriais.DataSource = Conexao.getInstance.executeQuery(sql);
            dtgrdvwMateriais.Columns[0].HeaderText = "Código";
        }

        private void dtgrdvwRequisicao_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //4
            if (dtgrdvwRequisicao.RowCount > 0)
            {
                if (dtgrdvwRequisicao.CurrentRow.Cells[4].Value == null || dtgrdvwRequisicao.CurrentRow.Cells[4].Value.ToString().Equals(""))
                {
                    dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "Aprovar";
                }
                else if (dtgrdvwRequisicao.CurrentRow.Cells[4].Value.Equals("Aprovar"))
                {
                    dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "Reprovar";
                }
                else
                {
                    dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "";
                }
            }
        }

        private void tlstrpmntmAprovar_Click(object sender, EventArgs e)
        {
            if (dtgrdvwRequisicao.RowCount > 0)
            {
                dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "Aprovar";
            }
        }

        private void tlstrpmntmReprovar_Click(object sender, EventArgs e)
        {
            if (dtgrdvwRequisicao.RowCount > 0)
            {
                dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "Reprovar";
            }
        }

        private void tlstrpmntmLimpar_Click(object sender, EventArgs e)
        {
            if (dtgrdvwRequisicao.RowCount > 0)
            {
                dtgrdvwRequisicao.CurrentRow.Cells[4].Value = "";
            }
        }

        public override bool metodo()
        {
            try
            {
                if (dtgrdvwRequisicao.RowCount == 0)
                {
                    return true;
                }
                foreach (DataGridViewRow r in dtgrdvwRequisicao.Rows)
                {
                    if (r.Cells[4].Value != null)
                    {
                        if (!r.Cells[4].Value.Equals(""))
                        {
                            string acao = "";
                            if (r.Cells[4].Value.Equals("Aprovar"))
                            {
                                acao = "1";
                            }
                            else
                            {
                                acao = "2";
                            }
                            List<String> alt = new List<string>();
                            alt.Add("Situacao = " + acao);
                            Conexao.getInstance.executeQuery(SqlCrud.getInstance.updateById(ConstTabelas.REQUISIÇÃO, alt, ConstTabelas.REQUISIÇÃO_PK, r.Cells[0].Value.ToString()));
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro no processo! \n" + ex.Message, "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
    }
}
